import Taro from '@tarojs/taro';

export interface CourseDt {
	id: number;
	type: number;
	thumbImgUrl: string;
	bannerImgUrl: string;
}

const data = {
	APP_NAME: '小蜜管家',
	freeApiKey: '', // 默认key
	bannerURL: '',
	isChatLimit: 1,
	erweima: '',
	isOpenGpt: false,
	clientId: Taro.getStorageSync('clientId'),
	shareImgUrl: '',

	courseList: [] as CourseDt[],

	// 页面共享数据
	pageShareInfo: {
		makeSamePrompt: '', // 制作同款的提示词
	}
}

// const isDev = true;
const isDev = false;
export const globalHost = isDev ? 'http://localhost:80' : 'https://wx.axises.fun'
export const globalWsHost = isDev ? 'ws://localhost:80' : 'wss://wx.axises.fun'

export const initGlobalData = () => {
	// console.log('初始化全局数据...')
	const db = Taro.cloud.database()

	if (data.courseList && data.courseList.length === 0) {
		db.collection('course').doc('fa6ae5786464ed1e000198990757d29d').get().then(res => {
			try {
				data.courseList = res.data.courseList
				// console.log('课程信息：', data.courseList)
			} catch (e) {
				console.log('解析课程信息异常：', e)
			}
		})
	}

	if (data.erweima === '') {
		db.collection('config').doc('fc8e6465644e36900a9e4d951d052b85').get().then(res => {
			try {
				data.erweima = res.data.erweima
				data.isOpenGpt = res.data.isOpenGpt
				data.shareImgUrl = res.data.shareImgUrl
				// console.log('二维码：', res)
			} catch (e) {
				console.log('解析课程信息异常：', e)
			}
		})
	}

	if (data.freeApiKey === '') {
		Taro.request({
			url: "https://wx.axises.fun/ai/getConfig",
			method: "POST",
			header: {
				'content-type': 'application/json' // 默认值
			},
			success: (res) => {
				console.log('获取配置信息成功:', res)
				data.freeApiKey = res.data.data.freeApiKey;
				data.bannerURL = res.data.data.bannerURL;
				data.isChatLimit = res.data.data.isChatLimit;
			},
			fail: (e) => {
				console.log('获取配置信息失败:', e)
			}
		})
	}
}


//         Taro.cloud.downloadFile({
//             fileID: 'cloud://gpt-2g802u5l527946e6.6770-gpt-2g802u5l527946e6-1317825570/banner.jpeg',
//             success: res => {
//                 // get temp file path
//                 console.log('获取云文件', res.tempFilePath)
//                 this.setState({
//                     bannerImageURL: res.tempFilePath, // 设置图像路径状态
//                 })
//             },
//             fail: err => {
//                 // handle error
//                 console.log('获取云文件失败', err)
//             }
//    })

export default data